<script lang="ts" setup>
  import { reactive, ref } from 'vue'
  import userService from '@/api/userService'
  import { encryptedData } from '@/utils/crypto'
  import appStore from '@/store'
  import { ElMessage } from 'element-plus'
  import type { FormInstance, FormRules } from 'element-plus'

  type user = {
    phoneNumber: string
    OldPassword: string
    newPassword: string
    dnewPassword: string
  }
  const formLabelAlign: user = reactive({
    phoneNumber: '',
    OldPassword: '',
    newPassword: '',
    dnewPassword: ''
  })
  const surePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
      callback(new Error('密码不能为空'))
    } else if (value !== formLabelAlign.newPassword) {
      callback(new Error('两次密码输入不一致'))
    } else {
      callback()
    }
  }
  const sureOldPass = (rule: any, value: any, callback: any) => {
    if (value === '') {
      callback(new Error('密码不能为空'))
    } else {
      callback()
    }
  }
  const rules = reactive<FormRules>({
    OldPassword: [
      {
        trigger: 'blur',
        validator: sureOldPass
      }
    ],
    newPassword: [
      { message: '请输入新密码', trigger: 'blur' },
      {
        pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@!%*#?&\.])[A-Za-z\d$@!%*#?&\.]{8,}$/,
        message: '至少8个字符，至少1个字母，1个数字和1个特殊字符(特殊字符包括：$@!%*#?&.)',
        trigger: 'blur'
      }
    ],
    dnewPassword: [{ trigger: 'blur', validator: surePass }]
  })
  const param = reactive({
    resconfig: {
      phoneNumber: '',
      oldPassword: '',
      newPassword: '',
      dnewPassword: ''
    }
  })

  appStore.userStore.isLogin = true

  const firstRef = ref<FormInstance>()
  const login = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
      if (valid) {
        param.resconfig = JSON.parse(JSON.stringify(formLabelAlign))
        userService.changePass(encryptedData(formLabelAlign.newPassword), encryptedData(formLabelAlign.OldPassword)).then((res) => {
          ElMessage.success('修改密码成功')
        })
      }
    })
}
</script>
<template>
  <div id="login">
    <div class="login-head">
      <div class="head-box">
      </div>
    </div>
    <div class="content-box">
      <div class="login-title"><span>找回密码</span></div>

      <div class="login-box">
        <el-form ref="firstRef" label-position="right" label-width="100px" :model="formLabelAlign" :rules="rules" style="margin-top: 40px">
          <el-form-item label-width="0" prop="oldPassword">
            <el-input v-model="formLabelAlign.OldPassword" type="password" style="width: 480px; height: 40px" show-password placeholder="请输入旧密码" />
          </el-form-item>
          <el-form-item label-width="0" prop="newPassword">
            <el-input v-model="formLabelAlign.newPassword" type="password" style="width: 480px; height: 40px" show-password placeholder="请输入新密码" />
          </el-form-item>
        </el-form>
        <button class="blogtn new" style="width: 480px; height: 40px; margin-top: 0px" @click="login(firstRef)">提交</button>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
  #login {
    height: 100vh;
    position: relative;
    .login-head {
      height: 90px;
      display: flex;
      align-items: center;
      margin-bottom: 70px;
      .head-box {
        height: 30px;
        display: flex;
        align-items: center;
        color: $text-color;
      }
    }

  }
  .content-box {
    background: $blank-color;
    height: 420px;
    width: 1200px;
    margin: 0 auto;
    .el-form-item {
      margin-bottom: 30px;
    }
    .login-title {
      height: 69px;
      display: flex;
      padding: 0 30px;
      align-items: center;
      font-size: 18px;
      font-weight: 700;
      color: $text-color;
      border-bottom: 1px solid $border-color;
      justify-content: space-between;
      
    }
    .login-box {
      margin: 0 auto;
      width: 480px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
</style>
